// miniprogram/pages/inspectiontask/addInsMain/addInsMain.js
var utils = require('../../../utils/getTool');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"添加巡检任务",
    mainData: { 
      id:0,
      inspectionTaskCode:'',
      line: { lineName:''},
      issuedByName:"admin",
      endPoleCode:'',
      startPoleCode:'',
      issuedByName:'',
      issuedByCode:''
    },
    solverNameList:[],
    inspectorDetailList:[],
    inspectorDetailName:""
  },
  toBak(){
    var pages = getCurrentPages(); //当前页面
    var beforePage = pages[pages.length - 2]; //前一页
    wx.navigateBack({
      success: function () {
        beforePage.onLoad(); // 执行前一个页面的onLoad方法
      }
    })
  },
  saveBtn() {
    if (this.data.title === "添加巡检任务"){ //如果你添加那么就要查询任务编码
      utils.req({ //根据你输入任务编码查询
        url: getApp().appData.site+"/accp/psInspectiontaskMain/fetchOne",
        data: {
          col: "inspectionTaskCode",
          val: this.data.mainData.inspectionTaskCode
        },
        success: res => {
          if (res.data.length >= 1) {
            wx.showToast({
              title: '该任务编码已存在!',
              icon: "none"
            })
            return
          }
        }
      })
    }
    var tda = this.data;
    console.log(tda)
    utils.req({
      url:getApp().appData.site+"/accp/psInspectiontaskMain/addMain",
      data:{ //日期格式需要转换无法直接传递值！！！ 
        inspectorDetailName: tda.inspectorDetailName,
        uid:tda.uid,
        id: tda.mainData.id,
        inspectionTaskCode: tda.mainData.inspectionTaskCode,
        inspectionTaskName: tda.mainData.inspectionTaskName,
        lineId: tda.mainData.line.id,
        lineCode: tda.mainData.line.lineCode,
        lineName: tda.mainData.line.lineName,
        startPole: tda.mainData.line.startPole.id,
        startPoleCode: tda.mainData.line.startPole.poleCode,
        endPole: tda.mainData.line.endPole.id,
        endPoleCode: tda.mainData.line.endPole.poleCode,
        issuedByCode: tda.mainData.issuedByCode,
        issuedByName: tda.mainData.issuedByName,
        common: tda.mainData.common
      },
      success:res=>{
        console.log(res)
        if (res.statusCode === 200){
          wx.showToast({
            title: '操作成功!',
            icon:"none"
          })
        }
      },fail:err=>console.log(err)
    })
    
  },
  btnSover(){
    var sdat = this.data.solverNameList;
    var uid = "";
    var inspectorDetailName = "";    
    var iName = this.data.inspectorDetailName;
    var arrName = [];
    iName = ""; //赋值之前先清除之前的赋值
    for (var i in sdat){
      iName += sdat[i].solverName +" ";
      arrName[i] = sdat[i].solverName;
      uid += sdat[i].userId + " ";      
    }
    this.setData({
      inspectorDetailList: arrName,
      inspectorDetailName: iName,
      show:false,
      uid: uid
    })
  },
  remLeftView() {
    var sList = this.data.solverNameList;
    var sList2 = this.data.solverNameList;
    for (var i in sList2) {
      if (sList2[i].sid === "R") {
        sList.splice(i, 1)
      }
    }
    this.setData({
      solverNameList: sList
    })
  },
  addLeftView(){
    var iList = this.data.getAllInctor;
    //只要你点击选择那么 删除之前所有沉于数据
    this.data.solverNameList = [];
    var sList = this.data.solverNameList;
    for (var i in iList) {
      if (iList[i].sid === this.data.mainData.id) {
        sList.push({
          solverName: iList[i].userName,
          userId: iList[i].id,
          sid: ""
        });
      }
    }
    this.setData({
      solverNameList: sList
    })
  },
  remSover(e){ //右边删除
    var index = e.target.dataset.index
    this.data.solverNameList[index].sid = 'R'
    console.log(this.data.solverNameList[index])
    this.setData({
      solverNameList: this.data.solverNameList
    })
  },
  remSoverLeft(e){
    var index = e.target.dataset.index
    this.data.solverNameList[index].sid = this.data.mainData.id
    console.log(this.data.solverNameList[index])
    this.setData({
      solverNameList: this.data.solverNameList
    })
  },
  seleSover(e) {//左边添加
    var index = e.target.dataset.index
    this.data.getAllInctor[index].sid = this.data.mainData.id
    this.setData({
      getAllInctor: this.data.getAllInctor
    })
  },
  selerem(e) {
    var index = e.target.dataset.index
    this.data.getAllInctor[index].sid = ""
    this.setData({
      getAllInctor: this.data.getAllInctor
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  getAllLineAndPole() {
    utils.req({
      url: getApp().appData.site+"/accp/psLine/getAllLineList",
      success: res => {
        console.log("得到所有杆塔信息", res)
        var list = [];        
        for (var i in res.data) {
          list.push({
            name: res.data[i].lineName,
            value: res.data[i]
          })
          if (this.data.temp && res.data[i].id == this.data.temp.lineId) {
            this.data.mainData.line = res.data[i]
            this.setData({mainData: this.data.mainData});
          }
        }
        this.setData({
          lineNameList: list
        })
      }, fail: err => console.log(e)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllLineAndPole();
    if(options.index){
      var temp = getApp().appData.inspectionList[options.index];
      this.setData({
        temp: temp
      })
      console.log(temp)
      utils.req({ //根据任务编号查询巡检员
        url:getApp().appData.site+"/accp/psInspectorDetail/fetchDetail",
        data:{
          column:"TASKID",
          val:temp.id
        },
        success:res=>{
          var sdat = this.data.solverNameList; // 右边显示的巡检员名称
          var iName = this.data.inspectorDetailName; // 显示的巡检员
          var arrName = []; //传递到后台
          iName = ""; //赋值之前先清除之前的赋值
          for (var i in res.data) {
            iName += res.data[i].solverName + " ";
            arrName[i] = res.data[i].solverName;
            sdat.push(res.data[i])
          }
          this.setData({
            inspectorDetailList: arrName,
            inspectorDetailName: iName,
            solverNameList: sdat,
            title:"修改巡检任务"
          })
        }
      })
      this.setData({
        mainData: temp
      })
    }
    this.setData({
      userInfo:wx.getStorageSync("userInfo")
    })
    this.data.mainData.issuedByCode = this.data.userInfo.id;
    this.data.mainData.issuedByName = this.data.userInfo.userName;
    utils.req({
      url:getApp().appData.site+"/accp/psUser/fetchCondition",
      data: {
        col: "roleId",
        value: "3"
      },
      success:res=>{
        console.log(res)
        var myFor = []
        for(var i in res.data){
          myFor.push(res.data[i])
        }
        this.setData({
          getAllInctor : res.data
        })        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onshowLine(){
    this.setData({
      showLine:true
    })
  },
  hideshowLine() {
    this.setData({
      showLine: false
    })
  },
  seleshowLine(e) {
    this.data.mainData.line = e.detail.value
    this.setData({
      mainData: this.data.mainData,
      showLine: false
    })
  },
  showInspector(){
    this.setData({
      show:true
    })
  },
  onInspectionTaskCode(e){
    this.data.mainData.inspectionTaskCode = e.detail
  },
  onInspectionTaskName(e) {
    this.data.mainData.inspectionTaskName = e.detail
  },
  tapComm(e){
    this.data.mainData.common = e.detail
  },
})